package com.tedu.egweibo3.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.egweibo3.base.response.JsonResult;
import com.tedu.egweibo3.mapper.WeiboMapper;
import com.tedu.egweibo3.pojo.dto.WeiboAddParam;
import com.tedu.egweibo3.pojo.entity.Weibo;
import com.tedu.egweibo3.pojo.vo.UserVO;
import com.tedu.egweibo3.pojo.vo.WeiboDetailVO;
import com.tedu.egweibo3.pojo.vo.WeiboIndexVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("v1/weibo")
@Tag(name = "微博模块")
@Validated
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("/insert")
    @Operation(summary = "发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userVO.getId());
        int num = weiboMapper.insert(weibo);
        return JsonResult.success();
    }
    @GetMapping("/selectIndex")
    @Operation(summary = "微博首页")
    @ApiOperationSupport(order =20)
    public JsonResult selectIndex(){
        List<WeiboIndexVO> weibolist = weiboMapper.selectList();
        return JsonResult.success(weibolist);
    }
    @GetMapping("/selectDetail")
    @Operation(summary = "微博详情")
    @ApiOperationSupport(order =30)
    public JsonResult selectDetail(@Range(min = 100,max = 500,message = "微博的编号id必须在100-500之间") Long id){
        WeiboDetailVO weiboDetailVO = weiboMapper.selectDetail(id);
        return JsonResult.success(weiboDetailVO);
    }
}
